package com.mixed;

import java.io.*;
import java.security.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.*;
import java.security.interfaces.*;
import java.security.spec.*;
//import org.apache.commons.codec.binary.Base64;
import javax.crypto.spec.*;

public class MixedDecrypt {

    //解密得到DES密钥
    public static String DESKeyDecrypt(String cipherpath, String keypath) throws Exception {
        String key = loadKeyByFile(keypath);
        BufferedReader br = new BufferedReader(new FileReader(cipherpath));
        String readLine = null;
        StringBuilder sb = new StringBuilder();
        while ((readLine = br.readLine()) != null) {
            sb.append(readLine);
        }
        br.close();
        String plain = decrypt(sb.toString(), key);
        return plain;
    }

    //从文件中读取RSA密钥
    public static String loadKeyByFile(String path) throws Exception {
        try {
            BufferedReader br = new BufferedReader(new FileReader(path));
            String readLine = null;
            StringBuilder sb = new StringBuilder();
            while ((readLine = br.readLine()) != null) {
                sb.append(readLine);
            }
            br.close();
            return sb.toString();
        } catch (IOException e) {
            throw new Exception("秘钥数据流读取错误");
        } catch (NullPointerException e) {
            throw new Exception("秘钥输入流为空");
        }
    }

    //RSA解密
    public static String decrypt(String signStr, String privateKey) throws Exception {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        RSAPrivateKey prk = null;
        byte[] buffer = base64Decoder.decodeBuffer(privateKey);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        prk = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, prk);
        byte[] content = cipher.doFinal(base64Decoder.decodeBuffer(signStr));
        return new String(content, "UTF-8");
    }

    //DES解密
    public static void decrypt(String cipherpath, String key, String plainpath) throws Exception{
        try{
            OutputStream out = new FileOutputStream(plainpath);
            byte[] keyBytes = (new BASE64Decoder()).decodeBuffer(key);
            DESedeKeySpec dks = new DESedeKeySpec(keyBytes);//实例化DES密钥
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");//实例化密钥工厂
            SecretKey k = skf.generateSecret(dks);//生成密钥
            Cipher cipher = Cipher.getInstance("DESede");   
            cipher.init(Cipher.DECRYPT_MODE, k);   
            InputStream is = new FileInputStream(cipherpath);   
            CipherOutputStream cos = new CipherOutputStream(out, cipher);   
            byte[] buffer = new byte[1024];   
            int r;   
            while ((r = is.read(buffer)) >= 0) {   
                System.out.println();  
                cos.write(buffer, 0, r);   
            }
            cos.flush();  
            cos.close();   
            out.close();   
            is.close();   
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        String key = DESKeyDecrypt(args[0], args[1]);
        decrypt(args[2], key, args[3]);
    }
}